package com.beemans.common.ext

import android.view.View
import com.chad.library.adapter.base.BaseDelegateMultiAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.delegate.BaseMultiTypeDelegate
import com.tiamosu.navigation.ext.isValid

/**
 * @author tiamosu
 * @date 2021/3/12
 */

/**
 * adapter 扩展，防止重复点击 item
 */
fun BaseQuickAdapter<*, *>.setNbOnItemClickListener(
    interval: Long = 500,
    action: (adapter: BaseQuickAdapter<*, *>, view: View, position: Int) -> Unit
) {
    setOnItemClickListener { adapter, view, position ->
        if (view.isValid(interval)) {
            action.invoke(adapter, view, position)
        }
    }
}

/**
 * adapter 扩展，防止重复点击 childItem
 */
fun BaseQuickAdapter<*, *>.setNbOnItemChildClickListener(
    interval: Long = 500,
    action: (adapter: BaseQuickAdapter<*, *>, view: View, position: Int) -> Unit
) {
    setOnItemChildClickListener { adapter, view, position ->
        if (view.isValid(interval)) {
            action.invoke(adapter, view, position)
        }
    }
}

/**
 * 设置代理扩展
 */
fun <T> BaseDelegateMultiAdapter<T, *>.addMultiTypeDelegate(
    itemType: (t: T) -> Int
): BaseMultiTypeDelegate<T>? {
    setMultiTypeDelegate(object : BaseMultiTypeDelegate<T>() {
        override fun getItemType(data: List<T>, position: Int): Int {
            return itemType.invoke(data[position])
        }
    })
    return getMultiTypeDelegate()
}